package com.ruoyi.legal.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.legal.domain.Work;
import com.ruoyi.legal.service.IWorkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工作日程Controller
 *
 * @author xwj
 * @date 2025-04-29
 */
@RestController
@RequestMapping("/legal/work")
@Api(tags = "工作日程")
public class WorkController extends BaseController {
    @Autowired
    private IWorkService workService;

    /**
     * 查询工作日程列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Work work) {
        startPage();
        List<Work> list = workService.selectWorkList(work);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Work work) {
        startOrderBy();
        List<Work> list = workService.selectWorkList(work);
        return success(list);
    }

    /**
     * 导出工作日程列表
     */
    @Log(title = "工作日程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Work work) {
        List<Work> list = workService.selectWorkList(work);
        ExcelUtil<Work> util = new ExcelUtil<Work>(Work.class);
        util.exportExcel(response, list, "工作日程数据");
    }

    /**
     * 获取工作日程详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id) {
        return success(workService.selectWorkById(id));
    }

    /**
     * 新增工作日程
     */
    @Log(title = "工作日程", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Work work) {
        SysUser user = getLoginUser().getUser();
        if (work.getId() == null) {
            work.setStatus("0");
            work.setCreateId(user.getUserId());
            work.setCreateTime(DateUtils.getNowDate());
            work.setCreateBy(user.getNickName());
            workService.insertWork(work);
        } else {
            work.setUpdateId(user.getUserId());
            work.setUpdateTime(DateUtils.getNowDate());
            work.setUpdateBy(user.getNickName());
            workService.updateWork(work);
        }

        return success(work);
    }

    /**
     * 修改工作日程
     */
    @Log(title = "工作日程", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Work work) {
        SysUser user = getLoginUser().getUser();
        work.setUpdateId(user.getUserId());
        work.setUpdateTime(DateUtils.getNowDate());
        work.setUpdateBy(user.getNickName());
        return success(workService.updateWork(work));
    }

    /**
     * 删除工作日程
     */
    @Log(title = "工作日程", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(workService.deleteWorkByIds(ids));
    }

    @Log(title = "工作日程", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Work work) {
        int num = workService.delete(work);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Work>> queryPage(@RequestBody Work work) {
        Pages<Work> page = workService.queryPage(work);
        return ResponseModel.of(page);
    }
}
